package com.ruoyi.keep.service.impl;

import com.ruoyi.keep.domain.UserBuyNumberCard;
import com.ruoyi.keep.domain.UserBuyRechargeCard;
import com.ruoyi.keep.domain.WxWallet;
import com.ruoyi.keep.mapper.UserBuyNumberCardMapper;
import com.ruoyi.keep.mapper.UserBuyRechargeCardMapper;
import com.ruoyi.keep.mapper.WxWalletMapper;
import com.ruoyi.keep.service.IUserBuyRechargeCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 用户购买充值卡Service业务层处理
 *
 * @author wxy
 * @date 2024-08-13
 */
@Service
public class UserBuyRechargeCardServiceImpl implements IUserBuyRechargeCardService {
    @Autowired
    private UserBuyRechargeCardMapper userBuyRechargeCardMapper;
    @Autowired
    private UserBuyNumberCardMapper userBuyNumberCardMapper;
    @Autowired
    private WxWalletMapper wxWalletMapper;

    /**
     * 查询用户购买充值卡
     *
     * @param id 用户购买充值卡主键
     * @return 用户购买充值卡
     */
    @Override
    public UserBuyRechargeCard selectUserBuyRechargeCardById(Long id) {
        return userBuyRechargeCardMapper.selectUserBuyRechargeCardById(id);
    }

    @Override
    public UserBuyRechargeCard selectUserBuyRechargeCardByUserId(Long userId) {
        return userBuyRechargeCardMapper.selectUserBuyRechargeCardByUserId(userId);
    }

    /**
     * 查询用户购买充值卡列表
     *
     * @param userBuyRechargeCard 用户购买充值卡
     * @return 用户购买充值卡
     */
    @Override
    public List<UserBuyRechargeCard> selectUserBuyRechargeCardList(UserBuyRechargeCard userBuyRechargeCard) {
        return userBuyRechargeCardMapper.selectUserBuyRechargeCardList(userBuyRechargeCard);
    }

    /**
     * 新增用户购买充值卡
     *
     * @param userBuyRechargeCard 用户购买充值卡
     * @return 结果
     */
    @Override
    public int insertUserBuyRechargeCard(UserBuyRechargeCard userBuyRechargeCard) {
        return userBuyRechargeCardMapper.insertUserBuyRechargeCard(userBuyRechargeCard);
    }

    /**
     * 修改用户购买充值卡
     *
     * @param userBuyRechargeCard 用户购买充值卡
     * @return 结果
     */
    @Override
    @Transactional
    public int updateUserBuyRechargeCard(UserBuyRechargeCard userBuyRechargeCard) {   //判断是否是新增
        newOrNot(userBuyRechargeCard);

        if (userBuyRechargeCard.getCardType() == null) {
//            //余额支付方式
            return expenditure(userBuyRechargeCard);
        } else {
//            //微信支付方式
            return wxExpenditure(userBuyRechargeCard);
        }


    }

    /* 判断是否是新增 */
    public void newOrNot(UserBuyRechargeCard userBuyRechargeCard) {
        if (userBuyRechargeCard.getCardType() != null && userBuyRechargeCard.getCardType() == 1) {
            UserBuyNumberCard numberCard = new UserBuyNumberCard();
            //将UserBuyRechargeCard的数据放入UserBuyNumberCard
            numberCard.setUserId(userBuyRechargeCard.getUserId());
            numberCard.setResidueNumber(userBuyRechargeCard.getCardNum());

            List<UserBuyNumberCard> isNullNumberCard = userBuyNumberCardMapper.selectUserBuyNumberCardList(numberCard);
            //判断用户次数卡中是否有数据，没有就新增，有就修改
            if (isNullNumberCard == null) {
                userBuyNumberCardMapper.insertUserBuyNumberCard(numberCard);
            } else {
                UserBuyNumberCard user = beforeNumberCard(userBuyRechargeCard);
                Integer oldResidueNumber = user.getResidueNumber();
                Integer newResidueNumber = oldResidueNumber + userBuyRechargeCard.getCardNum();
                numberCard.setResidueNumber(newResidueNumber);
                userBuyNumberCardMapper.updateUserBuyNumberCard(numberCard);
            }
        } else if (userBuyRechargeCard.getCardType() != null && userBuyRechargeCard.getCardType() == 2) {
            List<UserBuyRechargeCard> isNullRechargeCard = userBuyRechargeCardMapper.selectUserBuyRechargeCardList(userBuyRechargeCard);
            if (isNullRechargeCard == null) {
                //为空就新增
                userBuyRechargeCardMapper.insertUserBuyRechargeCard(userBuyRechargeCard);
            } else {
                //不为空修改
                UserBuyRechargeCard user = beforeRechargeCard(userBuyRechargeCard);
                Double oldResiduePrice = user.getResiduePrice();
                Double newResiduePrice = oldResiduePrice + userBuyRechargeCard.getMorePrice();
                user.setResiduePrice(newResiduePrice);
                userBuyRechargeCardMapper.updateUserBuyRechargeCard(user);
            }
        }
    }

    /* 扣除余额，判断余额是否充足 */
    public Integer expenditure(UserBuyRechargeCard userBuyRechargeCard) {
        //得到修改之前的用户数据
        UserBuyRechargeCard user = beforeRechargeCard(userBuyRechargeCard);

        //得到改之前的用户余额
        Double residuePrice = user.getResiduePrice();
        //将前余额和操作充值（+）或消费（-）进行求和得到修改后的数据
        Double newResiduePrice = residuePrice + userBuyRechargeCard.getResiduePrice();

        //判断余额是否充足
        if (newResiduePrice >= 0) {
            //将修改后的数据放入对象中
            userBuyRechargeCard.setResiduePrice(newResiduePrice);
        } else {
            throw new RuntimeException();
//            return -1;
        }

        //调用数据库操作
        return userBuyRechargeCardMapper.updateUserBuyRechargeCard(userBuyRechargeCard);
    }

    public Integer wxExpenditure(UserBuyRechargeCard userBuyRechargeCard) {
        //得到修改之前的用户微信钱包数据
        WxWallet wxWallet = wxWalletMapper.selectWxWalletByUserId(userBuyRechargeCard.getUserId());

        //得到改之前的用户余额
        Double oldBalance = wxWallet.getBalance();
        //将前余额和操作充值（+）或消费（-）进行求和得到修改后的数据
        Double newBalance = oldBalance + userBuyRechargeCard.getResiduePrice();

        //判断余额是否充足
        if (newBalance >= 0) {
            //将修改后的数据放入对象中
            wxWallet.setBalance(newBalance);
        } else {
            throw new RuntimeException();
//            return -1;
        }

        //调用数据库操作
        return wxWalletMapper.updateWxWallet(wxWallet);
    }

    public UserBuyRechargeCard beforeRechargeCard(UserBuyRechargeCard userBuyRechargeCard) {
        //得到修改之前的用户数据
        UserBuyRechargeCard user = null;
        if (userBuyRechargeCard.getId() != null) {
            user = userBuyRechargeCardMapper.selectUserBuyRechargeCardById(userBuyRechargeCard.getId());
        } else {
            List<UserBuyRechargeCard> userList = userBuyRechargeCardMapper.selectUserBuyRechargeCardList(userBuyRechargeCard);
            for (UserBuyRechargeCard userSingle : userList) {
                user = userSingle;
            }
        }
        return user;
    }

    public UserBuyNumberCard beforeNumberCard(UserBuyRechargeCard userBuyRechargeCard) {
        //得到修改之前的用户数据
        UserBuyNumberCard user = new UserBuyNumberCard();
        user.setUserId(userBuyRechargeCard.getUserId());
        List<UserBuyNumberCard> userList = userBuyNumberCardMapper.selectUserBuyNumberCardList(user);
        for (UserBuyNumberCard userSingle : userList) {
            user = userSingle;
        }
        return user;
    }

    /**
     * 批量删除用户购买充值卡
     *
     * @param ids 需要删除的用户购买充值卡主键
     * @return 结果
     */
    @Override
    public int deleteUserBuyRechargeCardByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return userBuyRechargeCardMapper.deleteBatchIds(idList);
    }

    /**
     * 删除用户购买充值卡信息
     *
     * @param id 用户购买充值卡主键
     * @return 结果
     */
    @Override
    public int deleteUserBuyRechargeCardById(Long id) {
        return userBuyRechargeCardMapper.deleteById(id);
    }
}
